home *** CD-ROM | disk | FTP | other *** search
/ Minami 78 / MINAMI78.iso / Extra / winamp53.exe / $R0 / Winamp Modern / scripts / notifier.m < prev    next >
Text File  |  2005-09-15  |  11KB  |  326 lines

  1. #include <lib/std.mi>
  2. #include "attribs.m"
  3.  
  4. Function reset();
  5. Function createNotifier();
  6. Function showNotifier(Int w);
  7. Function onNext();
  8.  
  9. Function Int fillNextTrackInfo(String corneroverride);
  10. Function Int fillCustomInfo(String customstring);
  11.  
  12. Function checkPref(int bypassfs);
  13.  
  14. Global Container notifier_container;
  15. Global Layout notifier_layout;
  16. Global Timer notifier_timer;
  17. Global String last_autotitle;
  18.  
  19. // ------------------------------------------------------------------------------
  20. // init
  21. // ------------------------------------------------------------------------------
  22. System.onScriptLoaded() {
  23.   initAttribs();
  24.   notifier_timer = new Timer;
  25. }
  26.  
  27. // ------------------------------------------------------------------------------
  28. // shutdown
  29. // ------------------------------------------------------------------------------
  30. System.onScriptUnloading() {
  31.   delete notifier_timer;
  32. }
  33.  
  34. // ------------------------------------------------------------------------------
  35. // called by the system when the global hotkey for notification is pressed
  36. // ------------------------------------------------------------------------------
  37. System.onShowNotification() {
  38.   if (checkPref(1)) return;
  39.   createNotifier();
  40.   String str;
  41.   if (getStatus() == STATUS_PLAYING) str = "Playing";
  42.   if (getStatus() == STATUS_PAUSED) str = "Playback Paused";
  43.   if (getStatus() == STATUS_STOPPED) str = "Playback Stopped";
  44.   showNotifier(fillNextTrackInfo(str));
  45.   complete; // prevents other scripts from getting the message
  46.   return 1; // tells anybody else that might watch the returned value that, yes, we implemented that
  47. }
  48.  
  49. // ------------------------------------------------------------------------------
  50. // called by the system when the title for the playing item changes, this could be the result of the player
  51. // going to the next track, or of an update in the track title
  52. // ------------------------------------------------------------------------------
  53. System.onTitleChange(String newtitle) {
  54.   if (last_autotitle == newtitle) return;
  55.   if (StrLeft(newtitle, 1) == "[") {
  56.     if (StrLeft(newtitle, 7) == "[Buffer" ||
  57.         StrLeft(newtitle, 4) == "[ICY") return;
  58.   }
  59.   last_autotitle = newtitle;
  60.   onNext();
  61. }
  62.  
  63. // ------------------------------------------------------------------------------
  64. // called by the system when the user clicks the next button
  65. // ------------------------------------------------------------------------------
  66. onNext() {
  67.   if (checkPref(0)) return;
  68.   createNotifier();
  69.   showNotifier(fillNextTrackInfo(""));
  70. }
  71.  
  72. // ------------------------------------------------------------------------------
  73. // called by the system when the user clicks the play button
  74. // ------------------------------------------------------------------------------
  75. System.onPlay() {
  76.   if (checkPref(0)) return;
  77.   createNotifier();
  78.   showNotifier(fillNextTrackInfo("Playing"));
  79. }
  80.  
  81. // ------------------------------------------------------------------------------
  82. // called by the system when the user clicks the pause button
  83. // ------------------------------------------------------------------------------
  84. System.onPause() {
  85.   if (checkPref(0)) return;
  86.   createNotifier();
  87.   showNotifier(fillCustomInfo("Playback Paused"));
  88. }
  89.  
  90. // ------------------------------------------------------------------------------
  91. // called by the system when the user clicks the pause button again
  92. // ------------------------------------------------------------------------------
  93. System.onResume() {
  94.   if (checkPref(0)) return;
  95.   createNotifier();
  96.   showNotifier(fillNextTrackInfo("Resuming Playback"));
  97. }
  98.  
  99. // ------------------------------------------------------------------------------
  100. // called by the system when the user clicks the play button
  101. // ------------------------------------------------------------------------------
  102. System.onStop() {
  103.   if (checkPref(0)) return;
  104.   createNotifier();
  105.   showNotifier(fillCustomInfo("End of Playback"));
  106. }
  107.  
  108. // ------------------------------------------------------------------------------
  109. // checks if we should display anything
  110. // ------------------------------------------------------------------------------
  111. Int checkPref(int bypassfs) {
  112.   if (!bypassfs && notifier_disablefullscreen_attrib.getData() == "1" && isVideoFullscreen()) return 1;
  113.   if (notifier_never_attrib.getData() == "1") return 1;
  114.   if (notifier_minimized_attrib.getData() == "1" && !isMinimized()) return 1;
  115.   if (notifier_windowshade_attrib.getData() == "1") {
  116.     if (isMinimized()) return 0;
  117.     Container c = getContainer("main");
  118.     if (!c) return 1;
  119.     Layout l = c.getCurLayout();
  120.     if (!l) return 1;
  121.     if (l.getId() != "shade") return 1;
  122.   }
  123.   return 0;
  124. }
  125.  
  126. // ------------------------------------------------------------------------------
  127. // fade in/out completed
  128. // ------------------------------------------------------------------------------
  129. notifier_layout.onTargetReached() {
  130.   int a = notifier_layout.getAlpha();
  131.   if (a == 255) {
  132.     notifier_timer.setDelay(StringToInteger(notifier_holdtime_attrib.getData()));
  133.     notifier_timer.start();
  134.   }
  135.   else if (a == 0) {
  136.     reset();
  137.   }
  138. }
  139.  
  140. // ------------------------------------------------------------------------------
  141. // hold time elapsed
  142. // ------------------------------------------------------------------------------
  143. notifier_timer.onTimer() {
  144.   stop();
  145.   if (notifier_layout.isTransparencySafe()) {
  146.     notifier_layout.setTargetA(0);
  147.     notifier_layout.setTargetSpeed(StringToInteger(notifier_fadeouttime_attrib.getData()) / 1000);
  148.     notifier_layout.gotoTarget();
  149.   } else {
  150.     reset();
  151.   }
  152. }
  153.  
  154. // ------------------------------------------------------------------------------
  155. // when notifier is clicked, bring back the app from minimized state if its minimized and focus it
  156. // ------------------------------------------------------------------------------
  157. notifier_layout.onLeftButtonDown(int x, int y) {
  158.   notifier_timer.stop();
  159.   notifier_layout.cancelTarget();
  160.   reset();
  161.   restoreApplication();
  162.   activateApplication();
  163. }
  164.  
  165. // ------------------------------------------------------------------------------
  166. // close the notifier window, destroys the container automatically because it's dynamic
  167. // ------------------------------------------------------------------------------
  168. reset() {
  169.   notifier_container.close();
  170.   notifier_container = NULL;
  171.   notifier_layout = NULL;
  172. }
  173.  
  174. // ------------------------------------------------------------------------------
  175. createNotifier() {
  176.   if (notifier_container == NULL) {
  177.     notifier_container = newDynamicContainer("notifier");
  178.     if (!notifier_container) return; // reinstall duh!
  179.     if (isDesktopAlphaAvailable())
  180.       notifier_layout = notifier_container.getLayout("desktopalpha");
  181.     else
  182.       notifier_layout = notifier_container.getLayout("normal");
  183.     if (!notifier_layout) return; // reinstall twice, man
  184.   } else {
  185.     notifier_layout.cancelTarget();
  186.     notifier_timer.stop();
  187.   }
  188. }
  189.  
  190. // ------------------------------------------------------------------------------
  191. showNotifier(int w) {
  192.   w = w + 32;
  193.   int x = getViewportWidth() + getViewportLeft() - w - 2;
  194.   int y = getViewportHeight() + getViewportTop() - 80 - 2;
  195.  
  196.   notifier_layout.resize(x, y, w, 80);
  197.   if (notifier_layout.isTransparencySafe()) {
  198.     notifier_layout.show();
  199.     notifier_layout.setTargetA(255);
  200.     notifier_layout.setTargetX(x);
  201.     notifier_layout.setTargetY(y);
  202.     notifier_layout.setTargetW(w);
  203.     notifier_layout.setTargetH(80);
  204.     notifier_layout.setTargetSpeed(StringToInteger(notifier_fadeintime_attrib.getData()) / 1000);
  205.     notifier_layout.gotoTarget();
  206.   } else {
  207.     notifier_layout.setAlpha(255);
  208.     notifier_layout.show();
  209.     notifier_timer.setDelay(StringToInteger(notifier_holdtime_attrib.getData()));
  210.     notifier_timer.start();
  211.   }
  212. }
  213.  
  214. // ------------------------------------------------------------------------------
  215. Int fillNextTrackInfo(String corneroverride) {
  216.   Int maxv = 0;
  217.   Int stream = 0;
  218.   Group p = notifier_layout;
  219.   Text plentry = p.findObject("plentry");
  220.   Text nexttrack = p.findObject("nexttrack");
  221.   Text _title = p.findObject("title");
  222.   Text album = p.findObject("album");
  223.   Text artist = p.findObject("artist");
  224.   Text endofplayback = p.findObject("endofplayback");
  225.  
  226.   DebugString("got callback for " + getPlayItemString(), 0);
  227.   if (StrLeft(getPlayItemString(), 7) == "http://") stream = 1;
  228.  
  229.   if (endofplayback) endofplayback.hide();
  230.  
  231.   if (plentry) { plentry.setText(integerToString(getPlaylistIndex()+1)+"/"+integerToString(getPlaylistLength())); plentry.show(); }
  232.   if (nexttrack) {
  233.     if (corneroverride == "") {
  234.       if (!stream) {
  235.         if (!isVideo())
  236.           nexttrack.setText("New track");
  237.         else
  238.           nexttrack.setText("New video");
  239.       }
  240.       else nexttrack.setText("On air");
  241.     } else nexttrack.setText(corneroverride);
  242.     nexttrack.show();
  243.   }
  244.   if (_title) {
  245.     String str;
  246.     if (!stream) {
  247.       _title.setXmlParam("ticker", "0");
  248.       _title.setXmlParam("display", "");
  249.       str = getPlayitemMetaDataString("title"); 
  250.       if (str == "") str = getPlayitemDisplayTitle();
  251.       String l = getPlayItemMetaDataString("length");
  252.       if (l != "") {
  253.         str += " (" + integerToTime(stringtointeger(l)) + ")";
  254.       }
  255.       _title.setText(str);
  256.     } else {
  257.       _title.setXmlParam("ticker", "1");
  258.       _title.setXmlParam("display", "songtitle");
  259.       _title.setText("");
  260.     }
  261.     _title.show(); 
  262.   }
  263.   if (artist) { 
  264.     if (!stream) {
  265.       if (isVideo())
  266.         artist.setText(""); 
  267.       else
  268.         artist.setText(getPlayitemMetaDataString("artist")); 
  269.     } else {
  270.       if (isVideo())
  271.         artist.setText("Internet TV"); 
  272.       else
  273.         artist.setText("Internet Radio"); 
  274.     }
  275.     artist.show(); 
  276.   }
  277.   if (album) { 
  278.     String str;
  279.     if (!stream && !isVideo()) {
  280.       album.setXmlParam("display", "");
  281.       str = getPlayitemMetaDataString("album");
  282.       String l = getPlayitemMetaDataString("track");
  283.       if (l != "" && l != "-1") str += " (Track " + l + ")";
  284.       album.setText(str); 
  285.     } else {
  286.       album.setText("");
  287.       album.setXmlParam("display", "songinfo");
  288.     }
  289.     album.show(); 
  290.   }
  291.   maxv = artist.getAutoWidth();
  292.   if (maxv < album.getAutoWidth()) maxv = album.getAutoWidth();
  293.   if (maxv < _title.getAutoWidth()) maxv = _title.getAutoWidth();
  294.   if (maxv < 128) maxv = 128;
  295.   if (maxv > getViewportWidth()/4) maxv = getViewportWidth()/4;
  296.   
  297.   return maxv;
  298. }
  299.  
  300. // ------------------------------------------------------------------------------
  301. Int fillCustomInfo(String customtext) {
  302.   Group p = notifier_layout;
  303.   Text plentry = p.findObject("plentry");
  304.   Text nexttrack = p.findObject("nexttrack");
  305.   Text _title = p.findObject("title");
  306.   Text album = p.findObject("album");
  307.   Text artist = p.findObject("artist");
  308.   Text endofplayback = p.findObject("endofplayback");
  309.  
  310.   if (plentry) { plentry.hide(); }
  311.   if (nexttrack) nexttrack.hide();
  312.   if (_title) { _title.hide(); }
  313.   if (artist) { artist.hide(); }
  314.   if (album) { album.hide(); }
  315.  
  316.   if (endofplayback) {
  317.     endofplayback.setText(customtext);
  318.     int aw = endofplayback.getAutoWidth();
  319.     endofplayback.show();
  320.     if (aw > 128)
  321.       return aw;
  322.   }
  323.   return 128;
  324. }
  325.  
  326.